package io.aircraft.modules.wms.service.impl;

import io.aircraft.common.constant.OperateConstants;
import io.aircraft.common.exception.ErrorCode;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.modules.wms.dao.SimulateDao;
import io.aircraft.modules.wms.dto.SimulateDTO;
import io.aircraft.modules.wms.entity.stored.*;
import io.aircraft.modules.wms.service.SimulateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SimulateServiceImpl implements SimulateService {
    @Autowired
    private SimulateDao simulateDao;

    @Override
    public ProcResult simulateByManual(SimulateDTO dto) {
        //1.过读码器
        if (OperateConstants.SIMULATE_BCR.equals(dto.getSimulateType())) {
            return simulateWithBcr(dto.getContNo(), dto.getPositionNo());
        }
        //2.上架
        if (OperateConstants.SIMULATE_UP.equals(dto.getSimulateType())) {
            return simulateWithUp(dto.getContNo(), dto.getPositionNo());
        }
        //3.下架
        if (OperateConstants.SIMULATE_DOWN.equals(dto.getSimulateType())) {
            return simulateWithDown(dto.getContNo(), dto.getPositionNo());
        }
        //4.到达
        if (OperateConstants.SIMULATE_ARRIVE.equals(dto.getSimulateType())) {
            return simulateWithArrive(dto.getContNo(), dto.getPositionNo());
        }
        //5.放行
        if (OperateConstants.SIMULATE_NOTICE.equals(dto.getSimulateType())){
            return simulateWithNotice(dto.getPositionNo(), dto.getContNo());
        }
        //5.未知
        return new ProcResult().result(ErrorCode.API_SERVER_ERROR, dto.getSimulateType());
    }

    @Override
    public ProcResult goByManual(Map<String, Object> params) {
        String positionNo=(String) params.get("positionNo");
        String amount=(String) params.get("amount");
        return simulateWithNotice(positionNo,amount);
    }

    /**
     * 模拟过读码器分配货位
     *
     * @param contNo 托盘条码
     * @param dvcNo  设备编号
     * @return
     */
    private ProcResult simulateWithBcr(String contNo, String dvcNo) {
        ProcBcrSimulateEntity parameters = new ProcBcrSimulateEntity();
        parameters.setContNo(contNo);
        parameters.setDvcNo(dvcNo);
        simulateDao.procWmsBcrSimulate(parameters);
        return new ProcResult().result(parameters.getCode(), parameters.getMsg());
    }

    /**
     * 模拟上架
     *
     * @param contNo   托盘条码
     * @param locateNo 货位编号
     * @return
     */
    private ProcResult simulateWithUp(String contNo, String locateNo) {
        ProcUpSimulateEntity parameters = new ProcUpSimulateEntity();
        parameters.setContNo(contNo);
        parameters.setLocateNo(locateNo);
        simulateDao.procWmsUpSimulate(parameters);
        return new ProcResult().result(parameters.getCode(), parameters.getMsg());
    }

    /**
     * 模拟下架
     *
     * @param contNo   托盘条码
     * @param locateNo 货位编号
     * @return
     */
    private ProcResult simulateWithDown(String contNo, String locateNo) {
        ProcDownSimulateEntity parameters = new ProcDownSimulateEntity();
        parameters.setContNo(contNo);
        parameters.setLocateNo(locateNo);
        simulateDao.procWmsDownSimulate(parameters);
        return new ProcResult().result(parameters.getCode(), parameters.getMsg());
    }

    /**
     * 模拟站台到达
     *
     * @param contNo    托盘条码
     * @param stationNo 站台信息
     * @return
     */
    private ProcResult simulateWithArrive(String contNo, String stationNo) {
        ProcArriveSimulateEntity parameters = new ProcArriveSimulateEntity();
        parameters.setContNo(contNo);
        parameters.setStationNo(stationNo);
        simulateDao.procWmsArriveSimulate(parameters);
        return new ProcResult().result(parameters.getCode(), parameters.getMsg());
    }

    /**
     * 模拟放行
     * @param stationNo 放行站点
     * @param amount 放行数量
     * @return 结果
     */
    private ProcResult simulateWithNotice(String stationNo,String amount){
        ProcNoticeSimulateEntity parameters=new ProcNoticeSimulateEntity();
        parameters.setStationNo(stationNo);
        parameters.setAmount(amount);
        simulateDao.procWmsNoticeSimulate(parameters);
        return new ProcResult().result(parameters.getCode(), parameters.getMsg());
    }
}
